<?php

class Application_Model_IconService
{
    private static $instance = null;
    
    public static function getInstance(){
        if (Application_Model_IconService::$instance == null)
            Application_Model_IconService::$instance = 
                new Application_Model_IconService();
        return Application_Model_IconService::$instance;
    }
    
    function createIcon($path, $x, $y=0) 
    { 
        $t = getimagesize ($path);
        $with=$t[0]; 
        $height=$t[1];
         
        switch ($t[2]) 
        { 
            case 1:  $type='GIF';  $img = imagecreatefromgif($path);  break; 
            case 2:  $type='JPEG';  $img = imagecreatefromjpeg($path);  break; 
            case 3:  $type='PNG';  $img = imagecreatefrompng($path);  break; 
        } 
        if($y==0) 
        {$y=$x*($height/$with);}
        
        $thumb = imagecreatetruecolor($x,$y); 
        imagecopyresampled($thumb, $img,0,0,0,0,$x,$y,$with,$height);
        return $thumb; 
    }
    
    public function getIconPath($typeDirectory, $pictureName, $options = null){
        $path = Application_Model_PathsService::getInstance()->get($typeDirectory);
        $iconsPath = Application_Model_PathsService::getInstance()->get($typeDirectory, true);
        $iconsWebPath = Application_Model_PathsService::getInstance()->get($typeDirectory, true, array('web' => true));
        if ($pictureName != null && file_exists($path . $pictureName)){
            $sizePicture=getimagesize ($path . $pictureName);
            $widthPicture = $sizePicture[0];
            $heightPicture = $sizePicture[1];
            $widthIcon = 100;
            $heightIcon = 100;
            
            if(isset($options['x']) && isset($options['y'])){
                $widthIcon = $options['x'];
                $heightIcon = $options['y'];
            } else if(isset($options['x']) && !isset($options['y'])){
                $widthIcon = $options['x'];
                $heightIcon = $widthIcon * $heightPicture / $widthPicture;
            } else if(!isset($options['x']) && isset($options['y'])){
                $heightIcon = $options['y'];
                $widthIcon = $heightIcon * $widthPicture / $heightPicture;
            }
            
            if (file_exists($iconsPath . $pictureName)){
                $sizeIcon=getimagesize ($iconsPath . $pictureName);
                if (abs($widthIcon - $sizeIcon[0]) > 1 || abs($heightIcon - $sizeIcon[1]) > 1){
                    unlink($iconsPath . $pictureName);
                } else {
                    return $iconsWebPath . $pictureName;
                }
            }
            
            $iconPicture = $this->createIcon(($path . $pictureName), $widthIcon, $heightIcon);
            
            if (imagegif($iconPicture, $iconsPath . $pictureName)){
                imagedestroy($iconPicture);
                return $iconsWebPath . $pictureName;
            }
            
            imagedestroy($iconPicture);
        } 
        
        if ($pictureName != null && file_exists($iconsPath . $pictureName)){
            unlink($iconsPath . $pictureName);
        }
        //TODO: заглушка на пусту картинку
        return "/no_picture";
    }
}